/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.model.request;

import com.workcell.workdata.im.annotation.CreateAction;
import com.workcell.workdata.im.constant.MessageAction;
import com.workcell.workdata.im.constant.MessageFormat;
import com.workcell.workdata.im.constant.MessageState;
import com.workcell.workdata.im.entity.ChatMessage;
import com.workcell.workdata.im.entity.GroupMessage;
import com.workcell.workdata.im.entity.Message;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@ApiModel
public class MessageForwardRequest implements Serializable {

	@ApiModelProperty("原消息title")
	private String title;

	@ApiModelProperty("原消息content")
	private String content;

	@ApiModelProperty("原消息extra")
	private String extra;

	@ApiModelProperty("原消息format")
	private Byte format = MessageFormat.TEXT;

	@ApiModelProperty("接收者集合({id:action})")
	@Size(min = 1,message = "接收者UID集合不能为空" ,groups = {CreateAction.class})
	private List<MessageReceiver> receiver;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getExtra() {
		return extra;
	}

	public void setExtra(String extra) {
		this.extra = extra;
	}

	public Byte getFormat() {
		return format;
	}

	public void setFormat(Byte format) {
		this.format = format;
	}

	public List<MessageReceiver> getReceiver() {
		return receiver;
	}

	public void setReceiver(List<MessageReceiver> receiver) {
		this.receiver = receiver;
	}

	public Message ofMessage(String action){
		Message message = create(action);
		message.setAction(action);
		message.setTitle(title);
		message.setContent(content);
		message.setExtra(extra);
		message.setState(MessageState.STATE_NOT_RECEIVED);
		message.setFormat(format);
		message.setCreateTime(new Date());
		return message;
	}

	private Message create(String action){

		if (Objects.equals(action, MessageAction.ACTION_0)){
			return new ChatMessage();
		}
		return new GroupMessage();
	}

	@ApiModel
	@Valid
	public static class MessageReceiver{

		@ApiModelProperty("消息接收者ID")
		@NotNull(message = "消息接收者ID不能空",groups = {CreateAction.class})
		private Long id;

		@ApiModelProperty("消息接类型")
		@NotEmpty(message = "消息接类型不能空",groups = {CreateAction.class})
		private String action;

		@ApiModelProperty("待返回的消息ID")
		private long mid;

		@ApiModelProperty("待返回的消息时间戳")
		private long timestamp;

		public long getId() {
			return id;
		}

		public void setId(long id) {
			this.id = id;
		}

		public String getAction() {
			return action;
		}

		public void setAction(String action) {
			this.action = action;
		}

		public long getMid() {
			return mid;
		}

		public void setMid(long mid) {
			this.mid = mid;
		}

		public long getTimestamp() {
			return timestamp;
		}

		public void setTimestamp(long timestamp) {
			this.timestamp = timestamp;
		}
	}
}
